package com.xqboss.apps.controller.synthesis;

import cn.hutool.core.map.MapUtil;
import com.xqboss.apps.dto.synthesis.*;
import com.xqboss.apps.service.synthesis.SynthesisService;
import com.xqboss.apps.vo.synthesis.SynthesisDetailVo;
import com.xqboss.apps.vo.synthesis.SynthesisPageVo;
import com.xqboss.apps.vo.synthesis.SynthesisStartVo;
import com.xqboss.apps.vo.user.UserNFTItemVo;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "合成活动-活动配置")
@RequestMapping("/apps/synthesis")
public class AppSynthesisController extends BaseController {

    @Autowired
    private SynthesisService synthesisService;

    /**
     * 合成活动 列表查询
     */
    @GetMapping("/list")
    @ApiOperation("合成活动列表")
    public R<List<SynthesisPageVo>> loadList(@Validated SynthesisPageDto request) {
        request.setShow(true);
        List<SynthesisPageVo> list = synthesisService.selectAppPage(request);
        return R.ok(list);
    }


    @GetMapping("/v2/detail/{synthesisId}")
    @ApiOperation("合成活动详情v2")
    public R<SynthesisDetailVo> loadDetailV2(@Validated
                                           @PathVariable
                                           @ApiParam("活动ID")
                                           @NotNull(message = "活动不存在！")
                                           Long synthesisId) {
        SynthesisDetailVo detailVo = synthesisService.getAppDetailV2(getUserId(), synthesisId);
        return R.ok(detailVo);
    }

    @GetMapping("/query/perm/{synthesisId}")
    @ApiOperation("验证权限")
    public R queryPerm(@Validated
                           @PathVariable
                           @ApiParam("活动ID")
                           @NotNull(message = "活动不存在！")
                           Long synthesisId) {
        synthesisService.queryPerm(getUserId(), synthesisId);
        return R.ok();
    }

    @GetMapping("/query/stock/{synthesisId}")
    @ApiOperation("库存查询")
    public R queryStock(@Validated
                            @PathVariable @ApiParam("活动ID")
                            @NotNull(message = "活动不存在！")
                            Long synthesisId) {
        int stock = synthesisService.queryStock(getUserId(), synthesisId);
        return R.ok(MapUtil.of("stock", stock));
    }

    @GetMapping("/consume/user/nft/list")
    @ApiOperation("消耗用户nft列表")
    public R<SynthesisPlanConsumeUserNftVo> consumeUserNftList(@Validated SynthesisPlanConsumeUserNftDto param) {
        param.setUserId(getUserId());
        return R.ok(synthesisService.consumeUserNftList(param));
    }

    @RepeatSubmit(interval = 1000, justUrl = true, message = "访问过于频繁，请稍后再试")
    @PostMapping("/v2/start")
    @ApiOperation("合成活动v2")
    public R<SynthesisStartVo> startSynthesisV2(@RequestBody @Validated SynthesisStartDto dto) throws Exception {
        SynthesisStartVo vo = synthesisService.startSynthesisV2(getUserId(), dto);
        return R.ok(vo);
    }

}

